package com.lan.mylibrary.ui;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lan.mylibrary.R;


/**
 * package：com.yijia.aidaigou.com.lan.mylibrary.util
 * classname: MyTitle
 * create time: 2018/3/29 14:32
 * author: Lan
 * desc : 标题栏的封装
 */
public class MyTitle {


    private ImageView img_title_left, img_title_right;


    /**
     * @param context  上下文
     * @param imgLeft  左边图标
     * @param title    标题
     * @param imgRight 右边图标
     * @param isFinish 左侧点击事件是否为关闭界面
     */
    public MyTitle(Activity context, int imgLeft, String title, int imgRight, boolean isFinish) {

        View view = context.findViewById(R.id.ly_title);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        img_title_left = (ImageView) view.findViewById(R.id.img_title_left);
        img_title_right = (ImageView) view.findViewById(R.id.img_title_right);
        RelativeLayout titleLayout = (RelativeLayout) view.findViewById(R.id.ly_title);

        if (imgLeft != 0) {
            img_title_left.setVisibility(View.VISIBLE);
            img_title_left.setImageResource(imgLeft);
        }

        if (imgRight != 0) {
            img_title_right.setVisibility(View.VISIBLE);
            img_title_right.setImageResource(imgRight);
        }

        if (title != null) {
            tv_title.setText(title);
        }


        if (isFinish) {
            if (img_title_left.getVisibility() == View.VISIBLE) {
                img_title_left.setOnClickListener(v -> context.finish());
            }
        }
    }


    //默认左边为返回图标 右边没有图标
    public MyTitle(Activity context, String title) {

        View view = context.findViewById(R.id.ly_title);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        img_title_left = (ImageView) view.findViewById(R.id.img_title_left);
        RelativeLayout titleLayout = (RelativeLayout) view.findViewById(R.id.ly_title);

        img_title_left.setImageResource(R.drawable.ic_back);

        if (title != null) {
            tv_title.setText(title);
        }

        img_title_left.setOnClickListener(v -> context.finish());


}


    /**
     * fragment 中创建
     *
     * @param context
     * @param imgLeft
     * @param title
     * @param imgRight
     */
    public MyTitle(View context, int imgLeft, String title, int imgRight) {

        TextView tv_title = (TextView) context.findViewById(R.id.tv_title);
        img_title_left = (ImageView) context.findViewById(R.id.img_title_left);
        img_title_right = (ImageView) context.findViewById(R.id.img_title_right);

        if (imgLeft != 0) {
            img_title_left.setVisibility(View.VISIBLE);
            img_title_left.setImageResource(imgLeft);
        }

        if (imgRight != 0) {
            img_title_right.setVisibility(View.VISIBLE);
            img_title_right.setImageResource(imgRight);
        }

        if (title != null) {
            tv_title.setText(title);
        }
    }


//    标题点击事件
//    private TextView tv_title;
//
//    public MyTitle setTitleClick(View.OnClickListener listener) {
//        if (tv_title.getVisibility() == View.VISIBLE) {
//            tv_title.setOnClickListener(listener);
//        }
//        return this;
//    }


    public MyTitle setLeftClick(View.OnClickListener listener) {
        if (img_title_left.getVisibility() == View.VISIBLE) {
            img_title_left.setOnClickListener(listener);
        }
        return this;
    }


    public MyTitle setRightClick(View.OnClickListener listener) {
        if (img_title_right.getVisibility() == View.VISIBLE) {
            img_title_right.setOnClickListener(listener);
        }
        return this;
    }


}
